package com.antilia.infomaresme.data.services.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.antilia.infomaresme.data.dao.interfaces.IImageInfosDao;
import com.antilia.infomaresme.data.dataobjects.ImageInfo;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.services.FolderBasedImageDetail;
import com.antilia.infomaresme.data.services.IImageService;
import com.antilia.infomaresme.data.services.ImageDetail;
import com.antilia.infomaresme.data.services.mock.FileUtils;
import com.google.inject.Inject;

public class FileSystemImageService implements IImageService {

	private static final long serialVersionUID = 1L;

	private File rootFolder;
	
	@Inject
	private IImageInfosDao imageInfosDao;
	
	public FileSystemImageService() {
		if(System.getProperty("com.antilia.test") != null) {
			rootFolder = new File("C:/temp/images");
		} else {
			rootFolder = new File("/home/infomar/images");
		}
		if(!rootFolder.exists()) {
			throw new IllegalArgumentException("Can't use image service!");
		}
	}

	public List<ImageDetail> getImageDetails(Item item) {
		List<ImageInfo> images = imageInfosDao.getImageInfos(item);		
		ArrayList<ImageDetail> details = new ArrayList<ImageDetail>();
		File  folder = new File(rootFolder,Long.toString(item.getId()));
		for(ImageInfo imageInfo : images) {
			ImageDetail  detail = new FolderBasedImageDetail(folder, imageInfo.getBlobId());
			detail.setBlobId(imageInfo.getBlobId());
			detail.setContentType(imageInfo.getContentType());
			details.add(detail);			
		}
		return details;
	}

	public ImageDetail resizeImage(ImageDetail imageDetail, int width, int height) {
		return imageDetail;
	}

	public ImageDetail storeImage(ImageInfo imageInfo, Item  item, byte[] image) {
		File  folder = new File(rootFolder,Long.toString(item.getId()));
		if(!folder.exists())
			folder.mkdirs();
		FolderBasedImageDetail  detail = new FolderBasedImageDetail(folder, imageInfo.getBlobId());
		try {
			FileUtils.copy(image, detail.getFile());
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return detail;
	}
	
	public boolean deleteImage(ImageInfo imageInfo, Item item) {
		File  folder = new File(rootFolder,Long.toString(item.getId()));
		if(!folder.exists())
			folder.mkdirs();
		FolderBasedImageDetail  detail = new FolderBasedImageDetail(folder, imageInfo.getBlobId());
		return detail.getFile().delete();
	}

	public ImageDetail getImageDetail(ImageInfo imageInfo, Item item) {
		File  folder = new File(rootFolder,Long.toString(item.getId()));
		if(!folder.exists())
			folder.mkdirs();
		FolderBasedImageDetail  detail = new FolderBasedImageDetail(folder, imageInfo.getBlobId());
		return detail;
	}
	
	public IImageInfosDao getImageInfosDao() {
		return imageInfosDao;
	}

	public void setImageInfosDao(IImageInfosDao imageInfosDao) {
		this.imageInfosDao = imageInfosDao;
	}

}
